// Copyright (C) 2019-2020 star.engine at outlook dot com
//
// This file is part of StarEngine
//
// StarEngine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// StarEngine is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with StarEngine.  If not, see <https://www.gnu.org/licenses/>.

#include "SWinMessages.h"

namespace Star {

WinMessages::WinMessages()
    : sub(subject.get_subscriber())
{}

WinMessages::~WinMessages() {
    sub.on_completed();
}

std::tuple<bool, LRESULT> WinMessages::message(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    Result result;
    auto m = Message{ hWnd, message, wParam, lParam, &result };
    try {
        sub.on_next(m);
    } catch (...) {
        sub.on_error(std::current_exception());
    }
    return std::make_tuple(result.handled, result.lres);
}

}
